package com.dalin.platform.loa.service.imple;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import com.dalin.common.pager.PageInfo;
import com.dalin.common.utils.DateUtils;
import com.dalin.common.utils.EncryptUtils;
import com.dalin.common.utils.NumberUtils;
import com.dalin.common.utils.StringUtils;
import com.dalin.framework.service.imple.FrameServiceImple;
import com.dalin.platform.loa.LoaConst;
import com.dalin.platform.loa.condition.LoaDicTypeCondition;
import com.dalin.platform.loa.condition.LoaProjectCondition;
import com.dalin.platform.loa.condition.LoaRoleCondition;
import com.dalin.platform.loa.condition.LoaRoleOperateCondition;
import com.dalin.platform.loa.condition.LoaUserCondition;
import com.dalin.platform.loa.model.LoaDicItemEntity;
import com.dalin.platform.loa.model.LoaDicTypeEntity;
import com.dalin.platform.loa.model.LoaMenuEntity;
import com.dalin.platform.loa.model.LoaOperateHeaderEntity;
import com.dalin.platform.loa.model.LoaOrganizationEntity;
import com.dalin.platform.loa.model.LoaProjectEntity;
import com.dalin.platform.loa.model.LoaRoleEntity;
import com.dalin.platform.loa.model.LoaRoleOperateEntity;
import com.dalin.platform.loa.model.LoaUserEntity;
import com.dalin.platform.loa.service.LoaService;
import com.dalin.platform.loa.vo.LoaDicItemTreeVo;
import com.dalin.platform.loa.vo.LoaDicTypeVo;
import com.dalin.platform.loa.vo.LoaOrganTreeVo;
import com.dalin.platform.loa.vo.LoaProjectVo;
import com.dalin.platform.loa.vo.LoaRoleVo;
import com.dalin.platform.loa.vo.LoaUserVo;

/*****************************************************************************
 * @content: 系统设置等通用接口具体实现                                                                                                *
 * @author : 那建林                                                                                                                                 *
 * @crtDate: 2010-05-12                                                     *
 * @version: 1.0                                                            *
 ****************************************************************************/
public class LoaServiceImple extends FrameServiceImple implements LoaService {
	
	public PageInfo queryLoaProjects(LoaProjectCondition cond) throws Exception {
        PageInfo pageInfo = queryPageInfoByCond(cond.prepareParams());
        List<LoaProjectEntity> entyList = pageInfo.getDataList();
        List<LoaProjectVo> voList = new ArrayList<LoaProjectVo>();
        LoaProjectVo vo = null;
        for (LoaProjectEntity enty : entyList) {
            vo = new LoaProjectVo();
	        vo.setProjectId(enty.getProjectId()); // 
	        vo.setProjectCode(enty.getProjectCode()); // 
	        vo.setProjectName(enty.getProjectName()); // 
	        vo.setProjectDescription(enty.getProjectDescription()); // 
	        vo.setCreateDate(DateUtils.getYmdhisStr(enty.getCreateDate()));
	        vo.setModifyDate(DateUtils.getYmdhisStr(enty.getModifyDate()));
	        voList.add(vo);
        }
        pageInfo.setDataList(voList);
        return pageInfo;
    }

    
    public LoaProjectVo findLoaProjectById(Long id) throws Exception {
        LoaProjectEntity enty = (LoaProjectEntity) getDao().findObjById(LoaProjectEntity.class, id);
        LoaProjectVo vo = new LoaProjectVo();
        vo.setProjectId(enty.getProjectId()); // 
        vo.setProjectCode(enty.getProjectCode()); // 
        vo.setProjectName(enty.getProjectName()); // 
        vo.setProjectDescription(enty.getProjectDescription()); // 
        return vo;
    }

    
    public void addLoaProject(LoaProjectVo vo) throws Exception {
        LoaProjectEntity enty = new LoaProjectEntity();
        enty = getLoaProjectEntityValue(enty, vo);
        enty.setCreateDate(new Date());
        enty.setValid(1);
        getDao().createObj(enty);
    }

    
    public void modifyLoaProject(LoaProjectVo vo) throws Exception {
        Long id = vo.getProjectId();
        LoaProjectEntity enty = (LoaProjectEntity) getDao().findObjById(LoaProjectEntity.class, id);
        enty = getLoaProjectEntityValue(enty, vo);
        enty.setModifyDate(new Date());
        getDao().updateObj(enty);
    }

   
    public void delLoaProjectsByIds(Long[] idArray) throws Exception {
    	LoaProjectEntity enty = null;
    	for (Long id : idArray) {
    		enty = (LoaProjectEntity) getDao().findObjById(LoaProjectEntity.class, id);
            enty.setModifyDate(new Date());
            enty.setValid(0);
            getDao().updateObj(enty);
    	}
    }

    private LoaProjectEntity getLoaProjectEntityValue(LoaProjectEntity enty, LoaProjectVo vo) {
        enty.setProjectCode(vo.getProjectCode()); // 
        enty.setProjectName(vo.getProjectName()); // 
        enty.setProjectDescription(vo.getProjectDescription()); // 
        return enty;
    }
	
    
    

    
    
    
    
    


	public boolean isExistLoginName(Long userId, String loginName)
			throws Exception {
    	// 用户名唯一校验 begin
    	boolean duplicateLoginName = false;
    	LoaUserCondition userCond = new LoaUserCondition();
    	userCond = new LoaUserCondition();
    	userCond.setLoginName(loginName);
    	List<LoaUserEntity> userEntyList = queryListByCond(
    			userCond.checkLoginParams());
    	for (int i = 0; i < userEntyList.size();i++) {
    		System.out.println("11111======="+userEntyList.get(i).getUserId());
    	}
    	if (userEntyList.size() > 0 && !userEntyList.get(0).getUserId()
		       		 .equals(userId)) {
	   		duplicateLoginName = true;
    	}
		return duplicateLoginName;
	}
	public PageInfo queryLoaUsers(LoaUserCondition cond) throws Exception {
        PageInfo pageInfo = queryPageInfoByCond(cond.prepareParams());
        List<LoaUserEntity> entyList = pageInfo.getDataList();
        List<LoaUserVo> voList = new ArrayList<LoaUserVo>();
        LoaUserVo vo = null;
        for (LoaUserEntity enty : entyList) {
            vo = new LoaUserVo();
	        vo.setUserId(enty.getUserId()); // 
	        vo.setRealName(enty.getRealName()); // 
	        vo.setSexName(LoaConst.findItemName(LoaConst.DIC_TYPE_CODE_ARRAY[7], enty.getSex()+""));
	        vo.setLoginName(enty.getLoginName()); //
	        vo.setOrganName(enty.getLoaOrganizationEntity().getOrganName());
	        voList.add(vo);
        }
        pageInfo.setDataList(voList);
        return pageInfo;
    }
    
    
    public LoaUserVo findLoaUserById(Long id) throws Exception {
        LoaUserEntity enty = (LoaUserEntity) getDao().findObjById(LoaUserEntity.class, id);
        LoaUserVo vo = new LoaUserVo();
        vo.setUserId(enty.getUserId()); // 
        vo.setOrganId(enty.getOrganId()); // 
        vo.setOrganName(enty.getLoaOrganizationEntity().getOrganName());
        vo.setLoginName(enty.getLoginName()); // 
        vo.setLoginPswd(enty.getLoginPswd()); // 
        vo.setRealName(enty.getRealName());
        vo.setSex(enty.getSex());
        return vo;
    }

    public Long addLoaUser(Long projectId, LoaUserVo vo) throws Exception {
        LoaUserEntity enty = new LoaUserEntity();
        enty = getLoaUserEntityValue(enty, vo);
        enty.setProjectId(projectId);
        enty.setFlagDel(1);
        enty.setCreateDate(new Date());
        enty.setOrganId(vo.getOrganId());
        getDao().createObj(enty);
        return enty.getUserId();
    }
    
    public List<List<String>> addLoaUsers(Long projectId,
			List<List<String>> dataList) throws Exception {
    	List<String> flagRow = dataList.get(1);
    	LoaUserVo vo = null;
		for (int i = 2; i < dataList.size(); i++) {
			List<String> row = dataList.get(i);
			vo = new LoaUserVo();
			for (int j = 0; j < flagRow.size(); j++) {
				String flagColumnValue = flagRow.get(j);
				String cv = row.get(j);
				if ("realName".equals(flagColumnValue)) {
					vo.setRealName(cv);
					vo.setLoginName(StringUtils.chineseToPinyin(cv).toLowerCase());
					vo.setLoginPswd(LoaConst.INITIALIZATION_PSWD);
				} else if ("sex".equals(flagColumnValue)) {
					vo.setSex(Integer.parseInt(LoaConst.findItemCode(
							LoaConst.DIC_TYPE_CODE_ARRAY[7], cv)));
				} else if ("organId".equals(flagColumnValue)) {
					vo.setOrganId(Long.parseLong(cv));
				}  else {
					row.add("列(" + flagColumnValue+ ")添加失败");
				}
			}
			try {
				addLoaUser(projectId, vo);
				dataList.remove(row);
				i--;
			} catch(Exception e) {
				e.printStackTrace();
				row.add("此行添加失败,请检查数据正确性");
			}
		}
		return dataList;
	}
    
    
    public void modifyLoaUser(LoaUserVo vo) throws Exception {
        LoaUserEntity enty = (LoaUserEntity) getDao().findObjById(
        		LoaUserEntity.class, vo.getUserId());
        enty = getLoaUserEntityValue(enty, vo);
        enty.setModifyDate(new Date());
        getDao().updateObj(enty);
    }

    public void delLoaUsers(Long[] delIdArray) throws Exception {
        getDao().deleteObjs(LoaUserEntity.class, delIdArray);
    }

    private LoaUserEntity getLoaUserEntityValue(
    		LoaUserEntity enty, LoaUserVo vo) {
    	enty.setRealName(vo.getRealName());
    	enty.setSex(vo.getSex());
        enty.setLoginName(vo.getLoginName()); // 
        if (!vo.getLoginPswd().equals(enty.getLoginPswd())) {
        	enty.setLoginPswd(EncryptUtils.encodeToMD40Str(vo.getLoginPswd()));
        }
        enty.setRemark(vo.getRemark());
        enty.setDefaultDataLevel(vo.getDefaultDataLevel());
        return enty;
    }
    
    
    public void addUserRoles(
    		Long userId, Long[] roleIdArray) throws Exception {
    	LoaUserEntity userEnty = (LoaUserEntity) getDao()
				.findObjById(LoaUserEntity.class, userId);
		LoaRoleEntity roleEnty = null;
		for (Long roleId : roleIdArray) {
			roleEnty = (LoaRoleEntity) getDao()
					.findObjById(LoaRoleEntity.class, roleId);
			userEnty.getLoaRoleEntitys().add(roleEnty);
		}
		getDao().updateObj(userEnty);
    }
    
    
    public void delUserRoles(
    		Long userId, Long[] roleIdArray) throws Exception {
    	LoaUserEntity userEnty = (LoaUserEntity) getDao()
    			.findObjById(LoaUserEntity.class, userId);
		LoaRoleEntity roleEnty = null;
		for (Long roleId : roleIdArray) {
			roleEnty = (LoaRoleEntity) getDao()
					.findObjById(LoaRoleEntity.class, roleId);
			userEnty.getLoaRoleEntitys().remove(roleEnty);
		}
		getDao().updateObj(userEnty);
    }




    
    public PageInfo queryLoaRoles(LoaRoleCondition cond) throws Exception {
        PageInfo pageInfo = queryPageInfoByCond(cond.prepareParams());
        List<LoaRoleEntity> entyList = pageInfo.getDataList();
        List<LoaRoleVo> voList = new ArrayList<LoaRoleVo>();
        LoaRoleVo vo = null;
        for (LoaRoleEntity enty : entyList) {
            vo = new LoaRoleVo();
	        vo.setRoleId(enty.getRoleId()); // 
	        vo.setProjectName(enty.getLoaProjectEntity().getProjectName());
	        vo.setRoleName(enty.getRoleName()); // 
	        vo.setRoleDescription(enty.getRoleDescription()); // 
	        vo.setCreateDate(DateUtils.getYmdhisStr(enty.getCreateDate()));
	        
	        vo.setAuthorized(LoaConst.NOT_AUTHORIZED);
	        if (cond.getUserId() != null) {
	        	Set userSet = enty.getLoaUserEntitys();
				for (Iterator it = userSet.iterator(); it.hasNext();) {
					LoaUserEntity userEnty = (LoaUserEntity) it.next();
					if (userEnty.getUserId().equals(cond.getUserId())) {
						vo.setAuthorized(LoaConst.IS_AUTHORIZED);
						break;
					} 
				}
	        }
	        voList.add(vo);
        }
        pageInfo.setDataList(voList);
        return pageInfo;
    }

    
    public LoaRoleVo findLoaRoleById(Long id) throws Exception {
        LoaRoleEntity enty = (LoaRoleEntity) getDao().findObjById(LoaRoleEntity.class, id);
        LoaRoleVo vo = new LoaRoleVo();
        vo.setRoleId(enty.getRoleId()); // 
        LoaProjectEntity lpe = enty.getLoaProjectEntity();
        vo.setProjectId(lpe.getProjectId());
        vo.setProjectName(lpe.getProjectName());
        vo.setRoleName(enty.getRoleName()); // 
        vo.setRoleDescription(enty.getRoleDescription()); // 
        return vo;
    }

  
    public void addLoaRole(LoaRoleVo vo) throws Exception {
        LoaRoleEntity enty = new LoaRoleEntity();
        enty.setProjectId(vo.getProjectId());
        enty.setCreateDate(new Date());
        enty = getLoaRoleEntityValue(enty, vo);
        getDao().createObj(enty);
    }

    
    public void modifyLoaRole(LoaRoleVo vo) throws Exception {
        LoaRoleEntity enty = (LoaRoleEntity) getDao()
        		.findObjById(LoaRoleEntity.class, vo.getRoleId());
        enty = getLoaRoleEntityValue(enty, vo);
        getDao().updateObj(enty);
    }

  
    public void delLoaRoles(Long[] idArray) throws Exception {
        getDao().deleteObjs(LoaRoleEntity.class, idArray);
    }
    
    
    public void addRoleMenus(Long roleId, String[] idArray) throws Exception {
    	List<Long> menuIdList = new ArrayList<Long>();
    	List<Long> operateIdList = new ArrayList<Long>();
    	List<Long> headerIdList = new ArrayList<Long>();
    	
    	for (String id : idArray) {
    		String flag = id.split("~")[0];
    		Long lid = Long.parseLong(id.split("~")[1]);
			if ("operate".equals(flag)) {
				operateIdList.add(lid);
			} else if ("header".equals(flag)) {
				headerIdList.add(lid);
			} else if ("menu".equals(flag)) {
				menuIdList.add(lid);
			}
    	}
    	/* 菜单 */
    	LoaRoleEntity roleEnty = (LoaRoleEntity) getDao()
    			.findObjById(LoaRoleEntity.class, roleId);
		roleEnty.getLoaMenuEntitys().clear();
		LoaMenuEntity menuEnty = null;
		for (Long menuId : menuIdList) {
			menuEnty = (LoaMenuEntity) getDao()
					.findObjById(LoaMenuEntity.class, menuId);
			roleEnty.getLoaMenuEntitys().add(menuEnty);
		}
		getDao().updateObj(roleEnty);
		
		/* 操作 + 列头　先删除，后增加修改 */
		List<LoaRoleOperateEntity> roeList = roleEnty.getLoaRoleOperateEntitys();
		for (int i = 0; i < roeList.size(); i++) {
			LoaRoleOperateEntity roeEnty = roeList.get(i);
			for (Long operateId : operateIdList) {
				if (operateId.equals(roeEnty.getOperateId())) {
					roeList.remove(roeEnty);
					i--;
					break;
				}
			}
		}
		getDao().deleteObjs(roeList);
		
		LoaRoleOperateCondition lroCond = new LoaRoleOperateCondition();
		lroCond.setRoleId(roleId);
		for (Long operateId : operateIdList) {
			lroCond.setOperateId(operateId);
			List<LoaRoleOperateEntity> tempList = getDao().queryObjsToPages(
					lroCond.prepareParams().getPreparedPageQuery());
			LoaRoleOperateEntity roeEnty = null;
			if (StringUtils.isBlank(tempList)) {
				roeEnty = new LoaRoleOperateEntity();
			} else {
				roeEnty = tempList.get(0);
			}
			roeEnty.setRoleId(roleId);
			roeEnty.setOperateId(operateId);
			StringBuffer sb = new StringBuffer();
			for (Long lohId : headerIdList) {
				LoaOperateHeaderEntity loheEnty = (LoaOperateHeaderEntity) getDao()
						.findObjById(LoaOperateHeaderEntity.class, lohId);
				if (loheEnty.getOperateId().equals(operateId)) {
					sb.append(lohId + ",");
				}
			}
			roeEnty.setLohIds(sb.toString());
			getDao().createObj(roeEnty);
		}
	}
    
    
    public void addRoleUsers(Long roleId, Long[] userIdArray) throws Exception {
    	LoaRoleEntity roleEnty = (LoaRoleEntity) getDao()
				.findObjById(LoaRoleEntity.class, roleId);
		roleEnty.getLoaUserEntitys().clear();
		if (userIdArray != null) {
			LoaUserEntity userEnty = null;
			for (Long userId : userIdArray) {
				userEnty = (LoaUserEntity) getDao()
						.findObjById(LoaUserEntity.class, userId); 
				roleEnty.getLoaUserEntitys().add(userEnty);
			}
		}
		getDao().updateObj(roleEnty);
    }
    
    private LoaRoleEntity getLoaRoleEntityValue(LoaRoleEntity enty, LoaRoleVo vo) {
        enty.setRoleName(vo.getRoleName()); // 
        enty.setRoleDescription(vo.getRoleDescription()); // 
        return enty;
    }
    
    
    
    
    
    
    public void addUpDelOrgans(List<LoaOrganTreeVo> treeList,
    		Long[] delIdArray) throws Exception{
    	Date currentDate = new Date();
		LoaOrganizationEntity enty = null;
		String parentId = "";
		int sequenceValue = 1;
		
		if (delIdArray != null && delIdArray.length > 0) {
			for (Long id : delIdArray) {
				if (id == null) continue;
				enty = (LoaOrganizationEntity) getDao()
						.findObjById(LoaOrganizationEntity.class, id);
				enty.setFlagDel(0);
				getDao().updateObj(enty);
			}
		}
		
		for (LoaOrganTreeVo vo : treeList) {
			if (NumberUtils.isIntegerType(vo.getParentId())) {
				parentId = vo.getParentId();
			}
			if (vo.getId().startsWith("newId-")) {
				enty = new LoaOrganizationEntity();
				enty.setProjectId(vo.getProjectId());
				enty.setOrganName(vo.getText());
				enty.setOrganType(vo.getOrganType());
				enty.setBelongAreaCode(vo.getBelongAreaCode());
				enty.setParentId(Long.parseLong(parentId));
				enty.setDescription(vo.getRemark());
				enty.setOrderNumber(sequenceValue++);
				enty.setFlagDel(1);
				enty.setCreateTime(currentDate);
				getDao().createObj(enty);
				parentId = enty.getOrganId()+"";
			} else {
				enty = (LoaOrganizationEntity) getDao().findObjById(
						LoaOrganizationEntity.class, Long.parseLong(vo.getId()));
				enty.setParentId(Long.parseLong(parentId));
				enty.setOrganName(vo.getText());
				enty.setOrganType(vo.getOrganType());
				enty.setBelongAreaCode(vo.getBelongAreaCode());
				enty.setDescription(vo.getRemark());
				enty.setOrderNumber(sequenceValue++);
				enty.setModifyTime(currentDate);
				getDao().updateObj(enty);
			}
		}
    }
    
    
    
    public PageInfo queryLoaDicTypes(LoaDicTypeCondition cond)throws Exception {
        PageInfo pageInfo = queryPageInfoByCond(cond.prepareParams());
        List<LoaDicTypeEntity> entyList = pageInfo.getDataList();
        List<LoaDicTypeVo> voList = new ArrayList<LoaDicTypeVo>();
        LoaDicTypeVo vo = null;
        for (LoaDicTypeEntity enty : entyList) {
            vo = new LoaDicTypeVo();
	        vo.setTypeId(enty.getTypeId()); // 
	        vo.setTypeCode(enty.getTypeCode()); // 
	        vo.setTypeDescription(enty.getTypeDescription()); // 
	        voList.add(vo);
	    }
        pageInfo.setDataList(voList);
        return pageInfo;
    }

  
    public LoaDicTypeVo findLoaDicTypeById(Long id)throws Exception {
        LoaDicTypeEntity enty = (LoaDicTypeEntity) getDao().findObjById(LoaDicTypeEntity.class, id);
        LoaDicTypeVo vo = new LoaDicTypeVo();
        vo.setTypeId(enty.getTypeId()); // 
        vo.setTypeCode(enty.getTypeCode()); // 
        vo.setTypeDescription(enty.getTypeDescription()); // 
        return vo;
    }

   
    public void addLoaDicType(LoaDicTypeVo vo) throws Exception {
        LoaDicTypeEntity enty = new LoaDicTypeEntity();
        enty = getLoaDicTypeEntityValue(enty, vo);
        getDao().createObj(enty);
    }

    
    public void addLoaDicTypeFromDicRepos(Long[] idArray, Long projectId) throws Exception {
//    	LoaDicTypeEntity typeEnty = null;
//    	LoaDicItemEntity itemEnty = null;
//    	GlbDicRepositoryEntity enty = null;
//    	LoaDicTypeCondition dicTypeCond = new LoaDicTypeCondition();
//    	dicTypeCond.setProjectId(projectId);
//    	for (Long id : idArray) {
//    		enty = (GlbDicRepositoryEntity) getDao()
//    				.findObjById(GlbDicRepositoryEntity.class, id);
//    		dicTypeCond.setTypeCode(enty.getTypeCode());
//    		List<LoaDicTypeEntity> entyList = queryListByCond(dicTypeCond.prepareParams());
//    		if (StringUtils.isBlank(entyList)) {
//    			typeEnty = new LoaDicTypeEntity();
//    			typeEnty.setProjectId(projectId);
//    			typeEnty.setTypeCode(enty.getTypeCode());
//    			typeEnty.setTypeDescription(enty.getTypeDescription());
//    			getDao().createObj(typeEnty);
//    		} else {
//    			typeEnty = entyList.get(0);
//    		}
//    		itemEnty = new LoaDicItemEntity();
//    		itemEnty.setTypeId(typeEnty.getTypeId());
//    		itemEnty.setItemCode(enty.getItemCode());
//    		itemEnty.setItemName(enty.getItemName());
//    		itemEnty.setParentItemCode(enty.getParentItemCode());
//    		itemEnty.setCreateDate(new Date());
//    		itemEnty.setOrderNumber(enty.getOrderNumber());
//    		itemEnty.setValid(1);
//    		getDao().createObj(itemEnty);
//    	}
    	
    }

    
    
    public void modifyLoaDicType(LoaDicTypeVo vo)throws Exception {
        Long id = vo.getTypeId();
        LoaDicTypeEntity enty = (LoaDicTypeEntity) getDao().findObjById(LoaDicTypeEntity.class, id);
        enty = getLoaDicTypeEntityValue(enty, vo);
        getDao().updateObj(enty);
    }

   
    public void delLoaDicTypesByIds(Long[] idArray)throws Exception {
        getDao().deleteObjs(LoaDicTypeEntity.class, idArray);
    }

    private LoaDicTypeEntity getLoaDicTypeEntityValue(LoaDicTypeEntity enty, LoaDicTypeVo vo) {
        enty.setProjectId(vo.getProjectId()); // 
        enty.setTypeCode(vo.getTypeCode()); // 
        enty.setTypeDescription(vo.getTypeDescription()); // 
        return enty;
    }
    
    
    public void addUpDelDicItems(Long typeId, List<LoaDicItemTreeVo> treeList, Long[] delIdArray) throws Exception {
    	Date currentDate = new Date();
		LoaDicItemEntity enty = null;
		String parentId = "";
		int maxItemCode = getDao().queryEqualObjs(LoaDicItemEntity.class, "typeId", typeId).size();
		int sequenceValue = 1;
		
		if (delIdArray != null && delIdArray.length > 0) {
			for (Long id : delIdArray) {
				if (id == null) continue;
				enty = (LoaDicItemEntity) getDao()
						.findObjById(LoaDicItemEntity.class, id);
				enty.setValid(0);
				getDao().updateObj(enty);
			}
		}
		
		for (LoaDicItemTreeVo vo : treeList) {
			if (NumberUtils.isIntegerType(vo.getParentId())) {
				parentId = vo.getParentId();
			}
			if (StringUtils.isBlank(vo.getHref())) {
				enty = new LoaDicItemEntity();
				enty.setTypeId(typeId);
				enty.setParentItemCode(parentId);
				enty.setItemCode(++maxItemCode + "");
				enty.setItemName(vo.getText());
				enty.setRemark(vo.getRemark());
				enty.setOrderNumber(sequenceValue++);
				enty.setValid(1);
				enty.setCreateDate(currentDate);
				getDao().createObj(enty);
				parentId = enty.getItemCode() + "";
			} else {
				enty = (LoaDicItemEntity) getDao().findObjById(
						LoaDicItemEntity.class, Long.parseLong(vo.getHref()));
				enty.setParentItemCode(parentId);
				enty.setItemCode(vo.getId());
				enty.setItemName(vo.getText());
				enty.setRemark(vo.getRemark());
				enty.setOrderNumber(sequenceValue++);
				enty.setModifyDate(currentDate);
				getDao().updateObj(enty);
			}
		}
    }
}